package com.xudow.app.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xudow.app.R;
import com.xudow.app.model.LocalMessage;
import com.xudow.app.util.MessageUtils;

import java.util.List;

/**
 * 我的消息ListView Adapter
 *
 * @author lukecheng
 */
public class MessageAdapter extends RecyclerView.Adapter {

    public interface OnRecyclerViewListener {
        void onItemClick(int position);

        boolean onItemLongClick(int position);
    }

    private OnRecyclerViewListener onRecyclerViewListener;

    public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }

    /**
     * 订单
     */
    private List<LocalMessage> messages;

    private Context context;

    public MessageAdapter(Context ctx, List<LocalMessage> messages) {
        this.context = ctx;
        this.messages = messages;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message_list, null);

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);

        return new MessageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MessageViewHolder viewHolder = (MessageViewHolder) holder;

        LocalMessage msg = messages.get(position);

        if (null != msg.getAgencyName()) {
            viewHolder.agency.setText(msg.getAgencyName());
        } else {
            viewHolder.agency.setText(context.getString(R.string.app_name));
        }

        String time = MessageUtils.formatTimeStampString(this.context, msg.getCreateTime());

        if (null != msg.getContent()) {
            viewHolder.message.setText(Html.fromHtml(msg.getContent()).toString().trim());
        }

        viewHolder.time.setText(time);
        viewHolder.title.setText(msg.getTitle());
    }

    @Override
    public int getItemCount() {
        return messages.size();
    }

    class MessageViewHolder extends RecyclerView.ViewHolder
            implements View.OnClickListener, View.OnLongClickListener {

        private LinearLayout root;
        public ImageView image;
        public TextView title;
        public TextView time;
        public TextView agency;
        public TextView message;

        public MessageViewHolder(View itemView) {
            super(itemView);

            root = (LinearLayout) itemView.findViewById(R.id.detail_item);
            root.setOnClickListener(this);
            root.setOnLongClickListener(this);

            image = (ImageView) itemView.findViewById(R.id.image);
            title = (TextView) itemView.findViewById(R.id.title);
            time = (TextView) itemView.findViewById(R.id.time);
            agency = (TextView) itemView.findViewById(R.id.agency_name);
            message = (TextView) itemView.findViewById(R.id.message);
        }

        @Override
        public void onClick(View v) {
            if (null != onRecyclerViewListener) {
                onRecyclerViewListener.onItemClick(this.getAdapterPosition());
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (null != onRecyclerViewListener) {
                return onRecyclerViewListener.onItemLongClick(this.getAdapterPosition());
            }

            return false;
        }
    }
}
